<?
/**
 * Core index (Engine accounts & modules).
 *
 * This is the general overview file - all accounts and modules.
 *
 * Please read the LICENSE file
 * @license 	GNU Lesser General Public License
 * @copyright 	oneDot only <we@onedotonly.be>
 * @author 		Koen Betsens <koen@onedotonly.be>
 *
 * @package 	Core
 *
 * @todo		Rename deprecated "Core" trough-out the engine to this use
 */
 
 $Core = new OVERVIEW();
 
?>
<!DOCTYPE html>
<html lang="<?=LANG?>">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta charset="utf-8" />
	<title>engine</title>
	<link rel="icon" href="<?=THEME_CTRL?>core/icon.png" />
		
	<link rel="stylesheet" href="<?=THEME_CTRL?>core/style.css" type="text/css" media="screen" />
	<link rel="stylesheet" href="<?=THEME_CTRL?>engine/assets.css" type="text/css" media="screen" />
	<script src="<?=THEME_CTRL?>../js/xhttp.js" type="text/javascript"></script>
	<script src="<?=THEME_CTRL?>../js/prototype.js" type="text/javascript"></script>

	<script type='text/javascript'>

	/////// BASE WIN CLASS
	var Win = function ( vars, class ) {
		
		id = "el." + ( Date.parse( new Date()) + Math.random());
		
		Object.extend( obj = new Element( "section", { id:id, class: class || "ivory" }), win );
		obj.vars = vars || {};
		
		return obj.init();
	}
	
	var win = {
		
		init		: function() {
		
			this.insert( new Element( "div", { class: "close" }).observe( "click", this.close.bind( this )));
			
			return this;
		},
		
		request	: function( param ) {
			
			new Ajax.Request( 'control.call.php', {
  				parameters: { set: Object.toJSON( param )},
  				onSuccess: this.receive.bind( this, param.transfer_type )
			});
			
			if( param.ref ) $( param.ref ).toggleClassName( "load" );
			
			return this;
		},
		
		receive		: function( type, transport ) {
    		
    		if( !type.indexOf( "js" )) {
    			json = transport.responseText.evalJSON();
    		
    			$( json.ref ).toggleClassName( "load" )[ json.func || "update" ]( json.value );
    		
    		} else this.insert( transport.responseText );			

  		},

		close		: function() { this.hide(); }
	};
	
	/////// CORE CLASS
	
	core	= {
		
		accounts		: <?= $Core->accounts( "json" ) ?>,
		modules			: <?= $Core->modules( "json" ) ?>,
		moduleslocal	: {},
		modulesfalse	: {},
		constructs		: [ "constr", "mod", "js", "css" ],
		
		init			: function() {

			this.accounts.each( function( acc ){

				acc.modsactive = 0;
				$H( acc.modlist ).each( function( mod ){

//					False.
					if(!( acc.modslocal && acc.modslocal[ mod.key ]) && !core.modules[ mod.key ] ) 
						core.modulesfalse[ mod.key ] = acc.name;
						
					else {
					
//						Local / Global.						
						target = ( acc.modslocal && acc.modslocal[ mod.key ] )? "moduleslocal" : "modules";
						
						if( !core[ target ][ mod.key ] )
								
								 core[ target ][ mod.key ] = { actions : {}, accounts : 1, files : []};
						else if(!core[ target ][ mod.key ].accounts ) {
								 core[ target ][ mod.key ].actions = {};
								 core[ target ][ mod.key ].accounts = 1;
						} else 	 core[ target ][ mod.key ].accounts += 1;
						
						$H( mod.value ).each( function( action ){ 

							acc.modsactive += $A( action.value ).size();
							if( !core[ target ][ mod.key ].actions[ action.key ] ) core[ target ][ mod.key ].actions[ action.key ] = 1;
							else core[ target ][ mod.key ].actions[ action.key ] += action.value.size();
							 
						});
					}
				});
			
			});
		},
		
		sectionAccounts	: function() {
			
			ul = new Element( "ul" );
			ul.insert( li(  new Element( "h1" ).insert( "Engine <strong>core</strong>" ), span(), span( "users" ), span( "pages" ), span( "mods" ), span( "size" )));
			
			core.accounts.each( function( acc ){
		
				if( acc.mods ) {
			
					// Calculate idle mods
					mods = span( acc.mods );
					if( acc.modsactive < acc.mods ) mods.insert( new Element( "a", { class: "count red" }).insert(  acc.mods - acc.modsactive ));
			
					// Calculate idle pages
					pages = span( acc.pages );
					if( acc.pagesidle ) pages.insert( new Element( "a", { class: "count red" }).insert(  acc.pagesidle ));
			
						ul.insert( li( span( acc.name ), span( acc.dbname ), span( acc.users ), pages, mods, size = span( acc.size, acc.root ).addClassName( "link" )).addClassName( "active" ));
				} else	ul.insert( li( span( acc.name ), span( acc.root ), span(""), span(""), span(""), size = span( acc.size, acc.root ).addClassName( "link" )));
		
				size.observe( "click", function( e ){
					e.findElement( "section" ).request({ transfer_type: "js", type: "core", action: "accountsize", ref: this.root }); }.bind( acc ));
			});
			
			return ul;
		},
		
		sectionModules	: function() {
			
			ul = new Element( "ul" );
			
//			Global modules
			ul.insert( li(  new Element( "h1" ).insert( "Modules" ), span( "" ), span( "actions in use" ), span( "accounts" ), span( "used" )));
			$H( core.modules ).each( core.sectionModules_addlines.bind( ul ));
			
//			Local modules
			ul.insert( li(  new Element( "h1" ).insert( "Local modules" ), span( "" ), span( "actions in use" ), span( "accounts" ), span( "used" )));
			$H( core.moduleslocal ).each( core.sectionModules_addlines.bind( ul ));
			
//			Modules false
			if( core.modulesfalse ) {		
			
				ul.insert( li(  new Element( "h1" ).insert( "Modules <strong>false</strong>" ), span( "" ), span( "account" ), span( "" ), span( "" )));
				$H( core.modulesfalse ).each( function( mod ){
								
					ul.insert( li( span( mod.key ), span( "" ), span( mod.value ), span( "" ), span( "" )));
				});
			}		
			return ul;
		},
		
		sectionModules_addlines	: function( mod ){

			files = "";
			mod.value.files.each( function( n ){ if( n != 1 ) files += "<i>" + core.constructs[ n ] + "</i>" });
				
			actions = [];
			modcount = 0;
			$H( mod.value.actions ).each( function( action ){ modcount += action.value; actions.push( action.key ); });
				
			this.insert( line = li( span( mod.key ), span( files ), span( actions.join( ", " )), span( mod.value.accounts ), span( modcount )));
			if( modcount ) line.observe( "click", core.sectionModule.bind( line, mod.key, mod.value, files )).addClassName( "active" );
		},
		
		sectionModule	: function( name, mod, files ) {
		
			document.body.insert({ top: new Win().insert( ul = new Element( "ul" ) )});
			
			ul.insert( li(  new Element( "h1" ).insert( name ), span(), span( "action" ), span( "pages" ), span( "used" )));
			
			actions = [];
			core.accounts.each( function( acc ) {
				
				first = null;
				
				
				if( acc.modlist && acc.modlist[ name ] )
					$H( acc.modlist[ name ]).each( function( action ){
						
						level = ( acc.modslocal && acc.modslocal[ name ] )? "local" : "global";

						ul.insert( li( span(( first != acc.name )? acc.name: "" ), span( ( first != acc.name )? level: "" ), span( action.key ), span( action.value.uniq().join( ", " ) ), span( action.value.size() )));
						first = acc.name;
						actions.push( action.key );
					});
			});
			
			if( actions )	ul.insert({ after: span( "actions: " + actions.uniq().join( ", " ))});
			if( files )		ul.insert({ after: span( "files: " + files )});
			
		}
		
	};
	
	/////// SWEETENERS
	span = function( str, id ){ return new Element( "span", { id: id || null }).update( str ); }
	div = function( str ){ return new Element( "div" ).update( str ); }
	li = function( str ){
		
		el = new Element( "li" );
		
		if( !arguments[1] )	el.update( arguments[0] );
		else 				$A( arguments ).each( function( n ){ el.insert( n )});
			
		return el;
	}
	
</script>

</head>
<body>

<script type='text/javascript'> 
	
	core.init();

	document.body.insert( section = new Win());
	section.insert( core.sectionAccounts());

	document.body.insert( section = new Win());
	section.insert( core.sectionModules());

</script>

<div class="stable"></div>

</body>
</html>